<template>
  <div class='container'>
    <el-dialog :title="BigTitle" :visible="showAddDialog" @close="cancel">
    <template>
      <el-form ref="addForm" :model="addForm" :rules="rules" label-width="100px">
      <el-form-item label="文章标题" prop="title">
        <el-input placeholder="请输入文章标签" v-model="addForm.title" size="small"></el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
         <quill-editor ref="type" class="pl-editor" :options="editorOption" v-model="addForm.articleBody"></quill-editor>
      </el-form-item>
      <el-form-item label="视频地址" prop="videoURL">
        <el-input size="small" v-model="addForm.videoURL"></el-input>
      </el-form-item>
    </el-form>
    </template>
        <el-row slot="footer" type="flex" justify="end" class="footer">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="confirm">确认</el-button>
        </el-row>
    </el-dialog>
  </div>
</template>

<script>
// 导入富文本编辑器样式和组件
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { add, update } from '@/api/hmmm/articles'
export default {
  watch: {
    articlesById () {
      this.addForm.title = this.articlesById.title
      this.addForm.articleBody = this.articlesById.articleBody
      this.addForm.videoURL = this.articlesById.videoURL
    }
  },
  computed: {
    BigTitle () {
      return this.articlesById.id ? '修改文章' : '新增文章'
    }
  },
  data () {
    return {
      // 富文本配置项修改
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        },
        placeholder: '' // 默认显示为空
      },
      addForm: {
        title: '', // 标题
        articleBody: '', // 文章正文
        videoURL: '' // 视频地址
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      }
    }
  },
  components: {
    quillEditor
  },
  props: {
    showAddDialog: {
      type: Boolean,
      default: false
    },
    articlesById: {
      type: Object,
      default: () => {}
    }
  },
  methods: {
    // 确认按钮 整体校验
    confirm () {
      this.$refs.addForm.validate(async isOK => {
        if (isOK) {
          try {
            if (this.articlesById.id) {
              await update({ ...this.addForm, id: this.articlesById.id })
              this.$message.success('修改成功')
            } else {
              await add(this.addForm)
              this.$message.success('新增成功')
            }
            this.$emit('closeAddDialog')
            this.cancel()
          } catch (error) {
            this.$message.error(error)
          }
        }
      })
    },
    // 取消按钮
    cancel () {
      this.$refs.addForm.resetFields()
      this.$emit('update:showAddDialog', false)
      this.articlesById.id = null
      this.addForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .ql-editor {
    height: 200px
}
</style>
